<?php

namespace services\models;


class oms {

    private $_cache;

    public function __construct($cache) {
        $this->_cache = $cache;
    }

    public function get_stock($sku) {

        $config = include ROOT_CONFIG;

        if ($this->_cache->cache('stock_' . $sku)) {
            return $this->_cache->cache('stock_' . $sku);
        } else {
            $sign = md5($sku . API_KEY);
            $url = $config['oms_api']['get_stock_api_link'] . "?sku={$sku}?sign={$sign}";
            $resource = curl_init();
            curl_setopt($resource, CURLOPT_URL, $url);
            curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
            $store = curl_exec($resource);

            if (!$store)
                throw new \Exception('cannot connect to service');

            $arr_json = $store ? json_decode($store, true) : null;

            if ($arr_json && $arr_json['result'] == 'ok') {
                $this->_cache->cache('stock_' . $sku, $arr_json['data']);
                return $arr_json['data'];
            }

            return null;
        }
    }

}

?>
